package com.hu.jie.mywallpager.apadter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.hu.jie.mywallpager.R;
import com.hu.jie.mywallpager.bean.GiftBean;
import com.hu.jie.mywallpager.bean.JsonList;
import com.hu.jie.mywallpager.bean.Seek;
import com.hu.jie.mywallpager.bean.SeekList;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by jie on 2016/7/6.
 */
public class MySeekAdapter extends BaseAdapter {
    private static final String TAG="MySeekAdapter";
    private Context mContext;
    private List<Object> mDatas;


    //标记当前位置上是哪种类型
    private final int TYPE_TITLE = 0;
    private final int TYPE_NORAML = 1;
    private final int TYPE_LIST = 2;
    private final int TYPE_COUNT = TYPE_LIST + 1;


    public MySeekAdapter(Context context, List<Object> mDatas) {
        this.mContext = context;
        this.mDatas = mDatas;
    }


    @Override
    public int getCount() {
        Log.i(TAG, "getCount: "+mDatas.size());
        return mDatas == null ? 0 : mDatas.size();

    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    //放回到当前位置上的类型
    @Override
    public int getItemViewType(int position) {
       if(position==0){
           return TYPE_TITLE;
       }else if(position==1){
           return  TYPE_NORAML;
       }else{
           return  TYPE_LIST;
       }
    }

    //类型的数量
    @Override
    public int getViewTypeCount() {
        return TYPE_COUNT;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //得到当前位置上的类型
        int itemType = getItemViewType(position);
        TitleHolder titleHolder = null;
        NormalHolder normalHolder = null;
        ListHolder listholder = null;

        if (convertView==null){
            switch (itemType){
                case TYPE_TITLE:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.view_search_top,null);
                    titleHolder = new TitleHolder(convertView);
                    convertView.setTag(titleHolder);
                    break;
                case TYPE_NORAML:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.view_search_banner,null);
                    normalHolder = new NormalHolder(convertView);
                    convertView.setTag(normalHolder);
                    break;
                case TYPE_LIST:
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_search_bottom,null);
                    listholder = new ListHolder(convertView);
                    convertView.setTag(listholder);
                    break;
            }
        }else {
            switch (itemType){
                case TYPE_TITLE:
                    titleHolder = (TitleHolder) convertView.getTag();
                    break;
                case TYPE_NORAML:
                    normalHolder = (NormalHolder) convertView.getTag();
                    break;
                case TYPE_LIST:
                    listholder = (ListHolder) convertView.getTag();
            }
        }

        //设置数据
        switch (itemType){
            case TYPE_TITLE:
                    Seek bean = (Seek) mDatas.get(position);
                Log.i(TAG, "position: "+position);
                    ImageLoader.getInstance().displayImage(bean.data.get(0).imgs.get(0),titleHolder.niv);
                    ImageLoader.getInstance().displayImage(bean.data.get(1).imgs.get(0),titleHolder.niv2);
                    ImageLoader.getInstance().displayImage(bean.data.get(2).imgs.get(0),titleHolder.niv3);
                    ImageLoader.getInstance().displayImage(bean.data.get(3).imgs.get(0),titleHolder.niv4);
                break;
            case TYPE_NORAML:
                SeekList bean1= (SeekList) mDatas.get(position);
                    normalHolder.tv_normal.setText("查看更多");
                    Log.i(TAG, "getView: "+bean1.data.Topic.get(0).cover_path);
                    ImageLoader.getInstance().displayImage(bean1.data.Topic.get(0).cover_path,normalHolder.niv1);
                    ImageLoader.getInstance().displayImage(bean1.data.Topic.get(1).cover_path,normalHolder.niv2);
                break;
            case TYPE_LIST:
                JsonList.FirstInnerBean bean2= (JsonList.FirstInnerBean) mDatas.get(position);
                    listholder.tv_listholder.setText(bean2.keyword);
                    ImageLoader.getInstance().displayImage(bean2.imgs.get(0),listholder.iv_4);
                    ImageLoader.getInstance().displayImage(bean2.imgs.get(1),listholder.iv_3);
                    ImageLoader.getInstance().displayImage(bean2.imgs.get(2),listholder.iv_2);
                    listholder.iv_1.setImageResource(R.mipmap.arrow);
                break;
        }
        return convertView;
    }

    //初始化控件
    private class TitleHolder {
        private TextView tv_title;
        private NetworkImageView niv,niv2,niv3,niv4;
        public TitleHolder(View itemView) {
//            tv_title = (TextView) itemView.findViewById(R.id.textview3);
//            niv = (NetworkImageView) itemView.findViewById(R.id.net1);
//            niv2 = (NetworkImageView) itemView.findViewById(R.id.net2);
//            niv3 = (NetworkImageView) itemView.findViewById(R.id.net3);
//            niv4 = (NetworkImageView) itemView.findViewById(R.id.net4);
        }
    }

    private class NormalHolder {
        private NetworkImageView niv1,niv2;
        private TextView tv_normal;
        public NormalHolder(View itemView) {
//            niv1 = (NetworkImageView) itemView.findViewById(R.id.imseek1);
//            niv2 = (NetworkImageView) itemView.findViewById(R.id.imseek2);
//            tv_normal = (TextView) itemView.findViewById(R.id.tvseek1);
        }
    }

    private class ListHolder {
        private TextView tv_listholder;
        private ImageView iv_1;
        private NetworkImageView iv_2,iv_3,iv_4;
        public ListHolder(View itemView) {
//            tv_listholder = (TextView) itemView.findViewById(R.id.seektextlist1);
//            iv_1 = (ImageView) itemView.findViewById(R.id.imageView);
//            iv_2 = (NetworkImageView) itemView.findViewById(R.id.imageView2);
//            iv_3 = (NetworkImageView) itemView.findViewById(R.id.imageView3);
//            iv_4 = (NetworkImageView) itemView.findViewById(R.id.imageView4);
        }
    }
}
